Conditions | 13 |
Paths | 13 |
Total Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like $.fn.symphonySelectable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** |
||
48 | objects.on('click.selectable', settings.items, function select(event) { |
||
49 | var item = $(this), |
||
50 | items = item.siblings().addBack(), |
||
51 | object = $(event.liveFired), |
||
52 | target = $(event.target), |
||
53 | selection, deselection, first, last; |
||
54 | |||
55 | // Ignored elements |
||
56 | if(target.is(settings.ignore)) { |
||
57 | return true; |
||
58 | } |
||
59 | |||
60 | // Remove text ranges |
||
61 | if(window.getSelection) { |
||
62 | window.getSelection().removeAllRanges(); |
||
63 | } |
||
64 | |||
65 | // Range selection |
||
66 | if((event.shiftKey) && items.filter('.selected').length > 0 && !object.is('.single')) { |
||
67 | |||
68 | // Select upwards |
||
69 | if(item.prevAll().filter('.selected').length > 0) { |
||
70 | first = items.filter('.selected:first').index(); |
||
71 | last = item.index() + 1; |
||
72 | } |
||
73 | |||
74 | // Select downwards |
||
75 | else { |
||
76 | first = item.index(); |
||
77 | last = items.filter('.selected:last').index() + 1; |
||
78 | } |
||
79 | |||
80 | // Get selection |
||
81 | selection = items.slice(first, last); |
||
82 | |||
83 | // Deselect items outside the selection range |
||
84 | deselection = items.filter('.selected').not(selection).removeClass('selected').trigger('deselect.selectable'); |
||
85 | deselection.find('input[type="checkbox"]:hidden:first').prop('checked', false); |
||
86 | |||
87 | // Select range |
||
88 | selection.addClass('selected').trigger('select.selectable'); |
||
89 | selection.find('input[type="checkbox"]:hidden:first').prop('checked', true); |
||
|
|||
90 | } |
||
91 | |||
92 | // Single selection |
||
93 | else { |
||
94 | |||
95 | // Press meta or ctrl key to adjust current range, otherwise the selection will be removed |
||
96 | if((!event.metaKey && !event.ctrlKey && settings.mode != 'additive' && !target.is('input')) || object.is('.single')) { |
||
97 | deselection = items.not(item).filter('.selected').removeClass('selected').trigger('deselect.selectable'); |
||
98 | deselection.find('input[type="checkbox"]:hidden:first').prop('checked', false); |
||
99 | } |
||
100 | |||
101 | // Toggle selection |
||
102 | if(item.is('.selected')) { |
||
103 | item.removeClass('selected').trigger('deselect.selectable'); |
||
104 | item.find('input[type="checkbox"]:hidden:first').prop('checked', false); |
||
105 | } |
||
106 | else { |
||
107 | item.addClass('selected').trigger('select.selectable'); |
||
108 | item.find('input[type="checkbox"]:hidden:first').prop('checked', true); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | }); |
||
113 | |||
132 |